package com.hqd.jjvm.rtda.heap.ref;

import com.hqd.jjvm.rtda.heap.JClass;
import com.hqd.jjvm.rtda.heap.classloader.JClassLoader;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.IOException;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class JSymRef {
	private String className;
	private JClass jClass;


	public JSymRef(JClass refJClass, String className) throws ClassNotFoundException {
		this.className = className;
		newJSymRef(refJClass, className);
	}

	private void newJSymRef(JClass refJClass, String className) throws ClassNotFoundException {
		JClassLoader loader = refJClass.getLoader();
		JClass jc = null;
		try {
			jc = loader.loadJClass(className);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!jc.isAccessibleTo(refJClass)) {
			throw new IllegalAccessError();
		}
		this.jClass = jc;
	}
}
